package zhxt.helmet.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhxt.helmet.dao.CameraDao;
import zhxt.helmet.dao.PopedomDao;
import zhxt.helmet.pojo.*;

import java.util.ArrayList;
import java.util.List;

@Service
public class CameraService {
    @Autowired
    CameraDao cameraDao;
    @Autowired
    PopedomDao popedomDao;
    //根据辖区查摄像头
    public List<Camera> getCameras(int provinceId, int cityId, int districtId, int blockId){
        List<Camera> cameraList = new ArrayList<>();
        if(blockId == 1){//无
            ;//空列表
        }
        else if(blockId > 2) {//块特定
            cameraList.addAll(cameraDao.selectByBlockId(blockId));
        }
        else if(districtId == 1){//块全部，区空
            ;
        }
        else if(districtId > 2){//块全部，区特定
            List<Block> blockList = new ArrayList<>();
            blockList.addAll(popedomDao.selectBlocksByDistrictId(districtId));
            for(Block block : blockList){
                cameraList.addAll(cameraDao.selectByBlockId(block.getId()));
            }
        }
        else if(cityId == 1){//区全部，市空
            ;
        }
        else if(cityId > 2){//区全部，市特定
            List<District> districtList = popedomDao.selectDistrictsByCityId(cityId);
            for(District district : districtList){
                List<Block> blockList = popedomDao.selectBlocksByDistrictId(district.getId());
                for(Block block : blockList){
                    cameraList.addAll(cameraDao.selectByBlockId(block.getId()));
                }
            }
        }
        else if(provinceId == 1){//市全部，省无

        }
        else if(provinceId > 2){//市全部，省特定
            List<City> cityList = popedomDao.selectCitiesByProvinceId(provinceId);
            for(City city : cityList){
                List<District> districtList = popedomDao.selectDistrictsByCityId(city.getId());
                for(District district : districtList){
                    List<Block> blockList = popedomDao.selectBlocksByDistrictId(district.getId());
                    for(Block block : blockList){
                        cameraList.addAll(cameraDao.selectByBlockId(block.getId()));
                    }
                }
            }
        }
        else{//省全部
            cameraList.addAll(cameraDao.selectAllCameras());
            /*
            List<Province> provinceList = popedomDao.selectAllProvinces();
            for(Province province : provinceList){
                List<City> cityList = popedomDao.selectCitiesByProvinceId(province.getId());
                for(City city : cityList){
                    List<District> districtList = popedomDao.selectDistrictsByCityId(city.getId());
                    for(District district : districtList){
                        List<Block> blockList = popedomDao.selectBlocksByDistrictId(district.getId());
                        for(Block block : blockList){
                            cameraList.addAll(cameraDao.selectByBlockId(block.getId()));
                        }
                    }
                }
            }*/
        }
        return cameraList;
    }
    //根据id查url
    public String getUrl(int id){
        return cameraDao.queryURLById(id);
    }
    //插入新摄像头
    public void addCamera(Camera camera){
        cameraDao.add(camera);
    }
    //修改摄像头
    public void updateCamera(Camera camera){
        cameraDao.update(camera);
    }
    public void deleteCamera(Camera camera){
        cameraDao.delete(camera);
    }
}
